/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: StaffLifecycle.java 46 2012-09-19 09:10:23Z zhuxiaopeng $
 * $Revision: 46 $
 * $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 * $Author: zhuxiaopeng $
 */
package daily.entity;

import java.util.*;

import javax.persistence.*;

import zhu.framework.entity.lifecycle.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 46 $ - $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 */
public class StaffLifecycle extends AbstractLifecycle<Staff> {

	public StaffLifecycle(final LifecycleContext lifecycleContext) {
		super(lifecycleContext);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see zhu.framework.entity.lifecycle.Lifecycle#getEntityType()
	 */
	@Override
	public Class<Staff> getEntityType() {
		return Staff.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see zhu.framework.entity.lifecycle.Lifecycle#newInstance()
	 */
	@Override
	public Staff newInstance() {
		return new Staff();
	}

	public Staff findByUserName(final String userName) {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Staff o ");
		buf.append(" where o.userName = :theUserName");
		buf.append(" and o.used = true");
		final Query q = getEntityManager().createQuery(buf.toString());
		q.setParameter("theUserName", userName);
		return getSingleQueryResult(q);
	}

	public Staff findByUserNameAndPassword(final String userName, final String password) {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Staff o ");
		buf.append(" where o.userName = :theUserName");
		buf.append(" and   o.password = :thePassword");
		buf.append(" and o.used = true");
		final Query q = getEntityManager().createQuery(buf.toString());
		q.setParameter("theUserName", userName);
		q.setParameter("thePassword", password);
		return getSingleQueryResult(q);
	}

	public List<Staff> findAllUsed() {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Staff o ");
		buf.append(" where o.used = true");
		buf.append(" order by o.index ");
		final Query query = getEntityManager().createQuery(buf.toString());
		return getQueryResultList(query);
	}

	public List<Staff> findAll() {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Staff o ");
		buf.append(" order by o.index ");
		final Query query = getEntityManager().createQuery(buf.toString());
		return getQueryResultList(query);
	}
}
